package carassius.DAL;

import java.sql.PreparedStatement;
import java.sql.Date;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 *
 * @author siebz0r
 */
public abstract class ActionRow
		extends Row<Integer>
{
	//<editor-fold defaultstate="collapsed" desc="Fields">
	private final int ID;
	private Date _date;
	private String _comment;
	private AquariumRow _aquariumRow;
	private final Object DATE_LOCK,
			COMMENT_LOCK,
			AQUARIUM_LOCK;
	//</editor-fold>

	//<editor-fold defaultstate="collapsed" desc="Constructors">
	public ActionRow(ResultSet resultSet) throws SQLException
	{
		super(resultSet.getInt("id"));
		ID = resultSet.getInt("id");
		_date = resultSet.getDate("date");
		_comment = resultSet.getString("comment");
		_aquariumRow = Database.getAquariumTable().getByPrimaryKey(resultSet.getInt("aquarium_id"));
		DATE_LOCK = new Object();
		COMMENT_LOCK = new Object();
		AQUARIUM_LOCK = new Object();
	}
	//</editor-fold>

	//<editor-fold defaultstate="collapsed" desc="Public Properties">
	public int getId()
	{
		return ID;
	}

	public AquariumRow getAquarium()
	{
		synchronized (AQUARIUM_LOCK)
		{
			return _aquariumRow;
		}
	}

	public boolean setAquarium(AquariumRow aquarium)
	{
		boolean result = false;
		synchronized (AQUARIUM_LOCK)
		{
			if (aquarium != _aquariumRow)
			{
				try
				{
					String sql = "UPDATE action SET aquarium_id = ? where id = ?";
					PreparedStatement st = Database.prepareStatement(sql);
					if (aquarium != null)
					{
						st.setInt(1, aquarium.getId());
					}
					else
					{
						st.setNull(1, java.sql.Types.INTEGER);
					}
					st.setInt(2, ID);

					if (result = (st.executeUpdate() == 1))
					{
						_aquariumRow = aquarium;
					}
				}
				catch (SQLException ex)
				{
					System.out.println(ex.toString());
				}
			}
		}
		return result;
	}

	public String getComment()
	{
		synchronized (COMMENT_LOCK)
		{
			return _comment;
		}
	}

	public boolean setComment(String comment)
	{
		boolean result = false;
		synchronized (COMMENT_LOCK)
		{
			if (comment == null ? _comment != null : !comment.equals(_comment))
			{
				try
				{
					String sql = "UPDATE action SET comment = ? where id = ?";
					PreparedStatement st = Database.prepareStatement(sql);
					if (comment != null)
					{
						st.setString(1, comment);
					}
					else
					{
						st.setNull(1, java.sql.Types.VARCHAR);
					}
					st.setInt(2, ID);

					if (result = (st.executeUpdate() == 1))
					{
						_comment = comment;
					}
				}
				catch (SQLException ex)
				{
					System.out.println(ex.toString());
				}
			}
		}
		return result;
	}

	public Date getDate()
	{
		synchronized (DATE_LOCK)
		{
			return _date;
		}
	}

	public boolean setDate(Date date)
	{
		boolean result = false;
		synchronized (DATE_LOCK)
		{
			if (date != _date)
			{
				try
				{
					String sql = "UPDATE action SET date = ? where id = ?";
					PreparedStatement st = Database.prepareStatement(sql);
					if (date != null)
					{
						st.setDate(1, date);
					}
					else
					{
						st.setNull(1, java.sql.Types.DATE);
					}
					st.setInt(2, ID);

					if (result = (st.executeUpdate() == 1))
					{
						_date = date;
					}
				}
				catch (SQLException ex)
				{
					System.out.println(ex.toString());
				}
			}
		}
		return result;
	}
	//</editor-fold>

	//<editor-fold defaultstate="collapsed" desc="Public Methods">
	@Override
	public boolean equals(Object obj)
	{
		if (obj == null)
		{
			return false;
		}
		if (getClass() != obj.getClass())
		{
			return false;
		}
		final ActionRow other = (ActionRow) obj;
		if (this.ID != other.ID)
		{
			return false;
		}
		if (this._date != other._date && (this._date == null || !this._date.equals(other._date)))
		{
			return false;
		}
		if ((this._comment == null) ? (other._comment != null) : !this._comment.equals(other._comment))
		{
			return false;
		}
		if (this._aquariumRow != other._aquariumRow && (this._aquariumRow == null || !this._aquariumRow.equals(other._aquariumRow)))
		{
			return false;
		}
		return true;
	}

	@Override
	public int hashCode()
	{
		int hash = 7;
		return hash;
	}

	@Override
	public String toString()
	{
		StringBuilder sb = new StringBuilder();
		if (_comment == null || _comment.isEmpty())
		{
			sb.append(ID);
			sb.append(" ");
		}
		else
		{
			if (_comment.length() < 8)
			{
				sb.append(_comment.subSequence(0, _comment.length()));
			}
			else
			{
				sb.append(_comment.substring(0, 8));
				sb.append("...");
			}
		}
		return sb.toString();
	}
	//</editor-fold>
}